<template>
	<view class="wrap" style="padding-bottom: 60px;">
		<u-tabs :list="tabList" :is-scroll="false" :current="current" @change="change"></u-tabs>
		<view class="search">
			<u-icon name="scan" size="50" suffixIconStyle="color: #909399" @click="scanCode" ></u-icon>
			<u-search style="width: 100%;" v-model="queryParam.orderParams" @custom="search" @search="search"></u-search>
		</view>
		<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="top" @down="downCallback" @up="loadData">
			<view v-for="(item,index) in list" :key="index">
				<u-card @click="navTo('/pages/sys/workbench/processHandoverDetails',item)" class="task-list-item" :border="false" padding="20" margin="20rpx">
					<view slot="head" style="display: flex;align-items: center;justify-content: space-between;">
						<view style="display: flex;align-items: center;font-size: 30rpx;">{{item.orderNumber || ''}}</view><view style="color: #999999;font-size: 22rpx;">{{item.orderDate || ''}}</view>
					</view>
					<view class="" slot="body">
						<u-row gutter="16">
							<u-col span="12">
								<view class="apply-text"><span>项目编号：</span>{{item.projectNumber || ''}}</view>
								<view class="apply-text"><span>客户名称：</span>{{ item.customerName || '' }}</view>
								<view class="apply-text"><span>地址：</span>{{item.address || ''}}</view>
								<view class="apply-text"><span>业主电话：</span>{{item.phone || ''}}</view>
								<view class="apply-text"><span>备注：</span>{{item.remark || ''}}</view>
								
								<view style="display: flex;">
									<view style="line-height: 70rpx;">成品仓位：</view>
									<u-input  size="small" style="width: 180rpx;" v-model="item.productWarehouse" :clearable="true"
										placeholder="请选择仓位" type="select" :select-open="item.showSpace"
										@click="showSpaceSelect(item,index)"></u-input>
								</view>
								
								<view class="apply-text"><span>附件：</span></view>
								<view style="padding: 20rpx;"  >
									<image v-for="(child,index) in getImages(item)" :key="index"  class="user-images" :src="child.replace(/\\/g,'/')"  @click.stop="previewImage(child)"></image>
								</view>
							</u-col> 
						</u-row>
					</view>
					<!-- <view class="apply-list-foot" slot="foot" style="text-align: right;color: #58ca93;">
						审批通过
					</view> -->
				</u-card>
			</view>
		</mescroll-uni>
		
		<u-picker title="请选择产品仓位" mode="selector" v-model="showSpace" :range="spaceList" rangeKey="title"
			@confirm="selectSpace"></u-picker>
	</view>
	
</template>

<script>
import MescrollMixins from '@/components/mescroll-uni/mescroll-mixins';
import ListMxin from './mixins/ListMxin';
import {getQueryString} from '@/common/common'
export default {
	mixins:[MescrollMixins,ListMxin],
	data() {
		return {
			tabList: [{
				name: '待完成'
			},{
				name: '已完成',
			}],
			showSpace:false,
			queryParam: {
				processStatus:1
			},
			current: 0,
			show: false,
			spaceList:[],
			spaceIndex:-1,
			url:{
				list:'appletProcessList'
			}
		}
	},
	onLoad() {
		this.getProductSpace()
	},
	methods: {
		getImages(item){
			return item.attachList || []
		},
		doo(r){
			console.log(r);
		},
		showSpaceSelect(item,index){
			this.spaceIndex = index
			this.showSpace = true
			item.showSpace = true
			this.list.splice(index,1,item)
		},
		getProductSpace(){
			this.$u.api.getProductSpaceList({}).then(res => {
				console.log(res,200);
				if (res.code === 200) {
					let arr = res.data || []
					this.spaceList = arr.map(v => {
						let obj = {
							title: v.postion,
							value: v.id
						}
						return obj
					})
					console.log(this.spaceList);
				} else {
					this.$u.toast(res.msg)
				}
			})
		},
		
		selectSpace(index){
			let item = this.list[this.spaceIndex]
			this.$set(item,'productWarehouseId',this.spaceList[index].value)
			this.$set(item,'productWarehouse',this.spaceList[index].title)
			this.list.splice(index,1,item)
			this.saveWarehouse(item)
		},
		/**
		 * @确认选择仓位
		 */
		saveWarehouse(item) {
			let param = {
				id: item.id, //销售单id
				productWarehouseId: item.productWarehouseId, //成品仓库id 
				productWarehouse: item.productWarehouse //成品仓库名称
			}
			uni.showModal({
				confirmText: '确定',
				cancelText: '取消',
				title: '是否确认仓位？',
				success: (res) => {
					if (res.confirm) {
						this.$u.api.saveWarehouse(param).then(res => {
							if (res.code === 200) {
								this.$u.toast('操作成功')
								this.list = []
								this.loadData({size:10,num:1})
							} else {
								this.$u.toast(res.msg)
							}
						})
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
		
				}
			})
		
		},
		scanCode() {
			// this.$refs.loadinToast.show({ ...this.toastParams });
			this.doScan();
		},
		doScan() {
			uni.scanCode({
				scanType: ["qrCode"],
				success: (res) => {
					console.log('success');
					let code = getQueryString(res.result,'orderNumber');
					//扫描编号
					this.queryParam.orderNumber = code
					this.list = []
					this.getList({num:1,size:10})
				}, 
				fail: (res) => {
					console.log('未识别到二维码');
					this.$refs.loadinToast.hide();
				},
			})
		},
		change(index) {
			this.current = index;
			this.list = []
			this.queryParam.processStatus = index+1
			this.loadData({num:1,size:10})
		},
		
		/**
		 * 
		 * @param {number} page 
		 * 获取列表数据
		 */
		 getList(page) {
			let params = {
				pageNum: page.num,
				pageSize: page.size
			}
			params = Object.assign(params,this.getQueryParams())
			//联网加载数据
			this.$u.api[this.url.list](params).then(res => {
				//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
				let data = res.data.list
				//设置列表数据
				if (res.code == 200) {
					for (let annItem of data) {
						this.list.push(annItem)
					}
				}
				if (page.num == 1) {
					this.list = []; //如果是第一页需手动制空列表
					this.list = this.list.concat(res.data.list); //追加新数据
					if(this.list.length === 1){
						this.navTo('/pages/sys/workbench/processHandoverDetails',this.list[0])
					}
				}
				this.mescroll.endBySize(data.length,res.data.total);
			}).catch(() => {
				//联网失败, 结束加载
				this.mescroll.endErr();
			})
		},
	}
	
}
</script>

<style lang="scss" scoped>
@import 'index.scss';
page {
	background-color: #f5f5f5;
}
.wrap .search{
	padding: 20rpx;
	display: flex;
	align-items: center;
	background: #ffffff;
	width: 100%;
}
.apply-text{
	font-size: 28rpx;
	color: #333333;
	span{
		color: #999999;
	}
}
.user-images{
	width: 80px;
	height:80px;
	margin-right: 8px;
}
.apply-list-foot{
	font-size: 28rpx;
}
.personnel-list{
	display: flex;
	align-items: center;
	flex-wrap:wrap;
	.personnel-user{
		position: relative;
		margin: 5px 9px 0;
	}
	.user-images{
		width: 48px;
		height:48px;
		margin-right:0;
		
	}
	.iconfont{
		position: absolute;
		top:-12px;
		right:-5px;
		color: #FE0100;
	}
}

.user-images{
	width: 80px;
	height:80px;
	margin-right: 8px;
}
</style>